package com.squareup.okhttp;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;


import com.photo.beans.Photo;
import com.upload.Filetype;
import com.upload.ImageUtil;
import com.upload.ImgUpload;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by zhangbo on 2016/3/31.
 * 用于处理耗时较长的网络请求(文件上传与图片下载)
 */
public class LongTimeAuzHttp extends OkHttp {

    public static final OkHttpClient longTimeOkHttpClient = new OkHttpClient();//用于处理文件上传与图片下载的超时限制
    private static final String FIXED_AUZ = "Basic ";

    static {
        OkHttpClient.Builder builder = longTimeOkHttpClient.newBuilder();
        builder.connectTimeout(55, TimeUnit.SECONDS);
        builder.readTimeout(30, TimeUnit.SECONDS);
        builder.writeTimeout(30, TimeUnit.SECONDS);
    }

    /**
     * 直接封装好的json做为参数
     *
     * @param url
     * @param json
     * @param responseCallback
     * @param tag
     */
    public static void post(String url, String json, RespondCallBack responseCallback, String tag) {
        if (TextUtils.isEmpty(json))
            return;
        String auzStr = FIXED_AUZ + SIG;
        RequestBody body = RequestBody.create(ContentType, json);
        Request request = commonHeaderBuilder().addHeader("Authorization", auzStr.replaceAll("\n", "")).url(url).tag(tag).post(body).build();
        enqueue(request, responseCallback);
    }


    /**
     * Post方式发起请求
     *
     * @param url
     * @param params
     * @param responseCallback
     */
    public static void post(String url, Params params, RespondCallBack responseCallback, String tag) {
        if (params == null) {
            params = new Params();
        }
        String BOUNDARY = UUID.randomUUID().toString();
        MediaType MEDIA_TYPE_PNG = MediaType.parse("multipart/form-data;boundary="+BOUNDARY);
        String auzStr = FIXED_AUZ + SIG;
        String toJson = buildBodyJson(url, params);
        String typeconte=MEDIA_TYPE_PNG + ";boundary=" + BOUNDARY;
        RequestBody body = RequestBody.create(MEDIA_TYPE_PNG, toJson);
        Request request = commonHeaderBuilder().addHeader("Authorization", auzStr.replaceAll("\n", "")).url(url)
                .tag(tag).post(body).build();
        enqueue(request, responseCallback);
    }

    //文件形式上传图片
    public static void Imgpost(String url, String imgpath, RespondCallBack responseCallback, String tag) {
        MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        File f = new File(imgpath);
        if (f != null) {
            //压缩操作，控制图片长宽与大小
            byte[] photoBytes = ImageUtil.compressImage(
                    ImageUtil.decodeScaleImage(imgpath, 450, 450), 86);
            try{
                FileOutputStream fos = new FileOutputStream(f);
                fos.write(photoBytes);
                fos.flush();
                fos.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
            builder.addFormDataPart("file",f.getName(),RequestBody.create(MEDIA_TYPE_PNG, f))
                    .addFormDataPart("token", "token");

        }
        MultipartBody requestBody = builder.build();
        Request request = new Request.Builder() .addHeader("X-UA", "android") .url(url) .post(requestBody) .build();
        enqueue(request, responseCallback);
    }


    /**
     * Get方式发送请求
     *
     * @param url
     * @param params
     * @param responseCallback
     */
    public static void get(String url, Params params, RespondCallBack responseCallback, String tag) {
        String reqUrl = paramsUrl(url, params);
        String auzStr = FIXED_AUZ + SIG;
        Request request = commonHeaderBuilder().addHeader("Authorization", auzStr.replaceAll("\n", "")).url(reqUrl)
                .tag(tag).build();
        enqueue(request, responseCallback);
    }

    public static void enqueue(final Request request, RespondCallBack responseCallback) {
        longTimeOkHttpClient.newCall(request).enqueue(CommonCallback.obtain(responseCallback));
    }



}
